package net.arogarth.aws.android.manager.views;

import java.util.ArrayList;
import java.util.List;

import net.arogarth.aws.android.lib.AmazonEc2;
import net.arogarth.aws.android.lib.AwsRequestException;
import net.arogarth.aws.android.lib.models.Ec2Snapshot;
import net.arogarth.aws.android.manager.R;
import net.arogarth.aws.android.manager.views.listener.ListClickListener;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Ec2SnapshotList extends Activity {
	public class Ec2SnapshotListAdapter extends BaseAdapter {

		private final List<Ec2Snapshot> instances = new ArrayList<Ec2Snapshot>();

		private Context _context;

		public Ec2SnapshotListAdapter(Context view, ListView list) {
			this._context = view;

			list.setAdapter(this);
			list.setOnItemLongClickListener(new ListClickListener());

			this.loadData();
		}

		private void loadData() {
			final ProgressDialog pd = ProgressDialog.show(this._context,
					"Bitte warten...", "Daten werden geladen...", true, false);

			Thread t = new Thread() {
				public void run() {
					AmazonEc2 aec2 = new AmazonEc2();
					try {
						List<Ec2Snapshot> _instances = aec2.descriptSnapshots();
						for (Ec2Snapshot instance : _instances) {
							instances.add(instance);
						}

						handler.sendEmptyMessage(0);
					} catch (final AwsRequestException e) {
						runOnUiThread(new Runnable() {
							public void run() {
								Toast.makeText(_context, e.getMessage(),
										Toast.LENGTH_SHORT).show();
								finish();
							}
						});
					}
				}

				private Handler handler = new Handler() {
					@Override
					public void handleMessage(Message msg) {

						notifyDataSetChanged();

						pd.dismiss();
					}
				};
			};
			t.start();
			//
		}

		public int getCount() {
			return this.instances.size();
		}

		public Ec2Snapshot getItem(int arg0) {
			return (Ec2Snapshot) this.instances.get(arg0);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final Ec2Snapshot instance = (Ec2Snapshot) getItem(position);

			final LinearLayout _ll = (LinearLayout) LayoutInflater.from(
					this._context).inflate(R.layout.ec2_snapshot_list_item,
					parent, false);

			String name = instance.getSnapshotId();

			final String title = name;

			((TextView) _ll.findViewById(R.id.instanceId)).setText(title);
			((TextView) _ll.findViewById(R.id.tvState)).setText(instance
					.getStatus());
			((TextView) _ll.findViewById(R.id.description)).setText(instance
					.getDescription());

			ImageView iv = ((ImageView) _ll.findViewById(R.id.instanceStatus));

			if (instance.getStatus().equalsIgnoreCase("completed")) {
				iv.setImageResource(R.drawable.greenled);
			} else if (instance.getStatus().equalsIgnoreCase("pending")) {
				iv.setImageResource(R.drawable.yellowled);
			} else {
				iv.setImageResource(R.drawable.redled);
			}
			return _ll;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.basic_list);
		final ListView lv = (ListView) findViewById(R.id.contentlist);
		new Ec2SnapshotListAdapter(Ec2SnapshotList.this, lv);
	}
}
